package com.calvin.study.config.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.calvin.study.base.ResponseModel;
@Component
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

	@Value("${spring.security.logintype}")
	private String loginType;

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {
		if (loginType.equalsIgnoreCase("JSON")) {
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(ResponseModel.error("请检查用户名和密码是否正确").toJson());
		} else {
			super.onAuthenticationFailure(request, response, exception);
		}
	}

}
